<template>
	<view>
		<view class="image-bg">
			<image class="logo-image" src="../../static/login-logo.png"></image>
		</view>
		<u-modal v-model="show" :content="content" :zoom="false" :mask-close-able="true"
			:show-cancel-button="true"><button slot="confirm-button" class="btn hairline-left"
				open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber">确定</button></u-modal>
		<view class="btn-box">
			<button class="btn-item item01" @click="getUserProfile">
				<view class="line-box">
					<image class="img" mode="scaleToFill" src="../../static/logo1.png"></image>
				</view>
				<text class="span">群众用户登录</text>
			</button>
			<button class="btn-item item02" @click="nameLogin">
				<view class="line-box">
					<image class="img img2" mode="scaleToFill" src="../../static/logo2.png"></image>
				</view>
				<text class="span">人大用户登录</text>
			</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show: false,
				jsCode: '',
				content: '请绑定手机号',
				infoRes: {
					avatarUrl: '',
					gender: 0,
					nickName: ''
				}
			};
		},
		methods: {

			nameLogin() {
				uni.navigateTo({
					url: './login'
				})
			},
			getUserProfile() {
				uni.login({
					provider: 'weixin',
					success: (res) => {
						if (res.code) {
							this.jsCode = res.code;
						} else {
							console.log('登录失败！' + res.errMsg);
						}
					}
				});
				// 推荐使用uni.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
				uni.getUserProfile({
					desc: '用于公众用户登录',
					success: (res) => {
						// this.$msg('请绑定手机号');
						this.infoRes = res.userInfo;
						this.show = true;
						console.log(this.show);
						console.log(this.infoRes);
					},
					fail: (res) => {
						console.log('getUserProfile:' + JSON.stringify(res))
						this.$msg('请同意获取信息用于公众用户登录');
						console.log(res);
					}
				});
			},
			onGetPhoneNumber(e) {
				if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
					//拒绝授权后弹出一些提示
					this.$msg('请同意获取手机号用于公众用户登录');
					console.log(e.detail);
				} else {
					this.encryptedData = e.detail.encryptedData; //加密的用户信息
					this.iv = e.detail.iv; //加密算法的初始向量
					// let pc = new WXBizDataCrypt('wxa3cad2b5781a0e48', this.session_key);
					// let data = pc.decryptData(e.detail.encryptedData, e.detail.iv);
					let data = {
						encryptedData: this.encryptedData,
						iv: this.iv,
						userInfo: this.infoRes,
						code: this.jsCode,
						//2023-4-8新增参数，与其他平台区分
						clientCode: this.$c.clientCode,
					};
					this.submit(data);
				}
			},
			submit(form) {
				this.$request('/wechat/login', form, {
					load: true
				}).then((res) => {
					if (!res.data.userId) {
						this.$msg('登录失败');
						return false;
					}
					let userInfo = res.data;
					userInfo.userInfo = this.infoRes;
					userInfo.roleCodes = ['qunzhong'];
					userInfo.roleNames = ['群众'];

					userInfo.sysUser.clientCode = userInfo.sysUser.clientCode || this.$c.clientCode;

					this.$store.commit('login', userInfo);
					uni.reLaunch({
						url: '/pages/load/index'
					});
				});
			}
		}
	}
</script>
<style>
	page {
		background: #ffffff;
	}
</style>
<style scoped lang="scss">
	.btn-box {
		// position: fixed;
		// left: 0;
		// right: 0;
		// bottom: 30rpx;
		margin: 180rpx 60rpx;

		.btn-item {

			display: flex;
			align-items: center;
			margin-top: 40rpx;
			padding: 0;
			height: 128rpx;
			font-size: 20px;
			color: #fff;
			line-height: 128rpx;
			text-align: center;
			text-indent: -1em;
			border-radius: 12rpx;
			border: 0;
			// box-shadow: 0 1rpx 6rpx 0 #666;

			&.item01 {
				background-color: #cd2d2c;
			}

			&.item02 {
				// opacity: 0;
				// z-index: -1;
				background-color: #ffb900;
			}

			&.item03 {
				// opacity: 0;
				// z-index: -1;
				background-color: #36adfd;
			}

			.img {
				flex-shrink: 0;
				padding: 32rpx 30rpx;
				width: 54rpx;
				height: 60rpx;

			}

			.img2 {
				width: 68rpx;
				height: 64rpx;
			}

			.span {
				width: 100%;
				text-align: center;
			}
		}
	}

	.line-box {
		background: rgba(255, 255, 255, 0.2);
		min-width: 128rpx;
		border-radius: 12rpx 0 0 12rpx;
		height: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
	}
</style>